package com.mylove.okhttp;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AppCompatActivity;

import com.benyanyi.loglib.Jlog;
import com.benyanyi.okhttp.OkHttpUtil;
import com.benyanyi.okhttp.download.DownloadInfo;
import com.benyanyi.okhttp.listener.OnDownLoadObserver;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author BenYanYi
 * @date 2018/11/29 14:59
 * @email ben@yanyi.red
 * @overview
 */
public class DownloadActivity extends AppCompatActivity implements View.OnClickListener {
    private Button downloadBtn1, downloadBtn2, downloadBtn3;
    private Button cancelBtn1, cancelBtn2, cancelBtn3;
    private ProgressBar progress1, progress2, progress3;
    String url = "http://apitest.yanyi.online/app/xiangyou.apk";

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_download);
        mContext = this;
        EventBus.getDefault().register(this);
        downloadBtn1 = bindView(R.id.main_btn_down1);
        downloadBtn2 = bindView(R.id.main_btn_down2);
        downloadBtn3 = bindView(R.id.main_btn_down3);

        cancelBtn1 = bindView(R.id.main_btn_cancel1);
        cancelBtn2 = bindView(R.id.main_btn_cancel2);
        cancelBtn3 = bindView(R.id.main_btn_cancel3);

        progress1 = bindView(R.id.main_progress1);
        progress2 = bindView(R.id.main_progress2);
        progress3 = bindView(R.id.main_progress3);

        downloadBtn1.setOnClickListener(this);
        downloadBtn2.setOnClickListener(this);
        downloadBtn3.setOnClickListener(this);

        cancelBtn1.setOnClickListener(this);
        cancelBtn2.setOnClickListener(this);
        cancelBtn3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_btn_down1:
//                AppContext.getDownloadManager().start(url2);
                break;
//            case R.id.main_btn_down1:
//                OkHttpUtil.getInstance(mContext).url(url1).download().start(new OnDownLoadObserver() {
//                    @Override
//                    public void onNext(DownloadInfo downloadInfo) {
//                        super.onNext(downloadInfo);
//                        JLog.v(downloadInfo.getProgress());
//                        progress1.setMax((int) downloadInfo.getTotal());
//                        progress1.setProgress((int) downloadInfo.getProgress());
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
//                break;
            case R.id.main_btn_down2:
                OkHttpUtil.getInstance(mContext).url(url).download(true).start(new OnDownLoadObserver() {
                    @Override
                    public void onNext(DownloadInfo downloadInfo) {
                        Jlog.v(downloadInfo.getProgress());
                        progress2.setMax((int) downloadInfo.getTotal());
                        progress2.setProgress((int) downloadInfo.getProgress());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
                break;
//            case R.id.main_btn_down3:
//                OkHttpUtil.getInstance(mContext).url(url3).download(".apk").start(new OnDownLoadObserver() {
//                    @Override
//                    public void onNext(DownloadInfo downloadInfo) {
//                        JLog.v(downloadInfo.getProgress());
//                        progress3.setMax((int) downloadInfo.getTotal());
//                        progress3.setProgress((int) downloadInfo.getProgress());
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
//                break;
            case R.id.main_btn_cancel1:
//                AppContext.getDownloadManager().cancel(url);
//                OkHttpUtil.getInstance(mContext).url(url1).download().cancel();
                break;
            case R.id.main_btn_cancel2:
                OkHttpUtil.getInstance(mContext).url(url).download(true).pause();
                break;
            case R.id.main_btn_cancel3:
                OkHttpUtil.getInstance(mContext).url(url).download(true).cancel();
                break;
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void download(DownloadInfo downloadInfo) {
        if (downloadInfo != null) {
            if (downloadInfo.getUrl().equals(url)) {
                progress1.setMax((int) downloadInfo.getTotal());
                progress1.setProgress((int) downloadInfo.getProgress());
            }
        }
    }

    private <T extends View> T bindView(@IdRes int id) {
        View viewById = findViewById(id);
        return (T) viewById;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
